using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;

namespace rajitWorkTwo
{
    /// <summary>
    /// This is the main type for your game
    /// </summary>
    
    public class Game1 : Microsoft.Xna.Framework.Game
    {
        GraphicsDeviceManager graphics;
        SpriteBatch spriteBatch;
        
        //static Galil.Galil g;
               
        // TEXTURE FOR BACKGROUND IMAGE
        Texture2D toolBG;
        
        // VIEWING RECTANGLE
        Rectangle viewportRect;

        // WORKLOG INFORMATION
        workLog workLogOne;
        Vector2 workLogPosition = new Vector2(400.0f, 200.0f + ((135.0f - gVar.workLogPixelHeight) / 2));

        // INPUT HELPER CLASS FOR TAKING CARE OF INPUT FROM KEYBOARD
        InputHelper input = new InputHelper();

        // TOOL INFORMATION
        tool toolOne;
        Vector2 toolPosition = new Vector2(102.0f, 280.0f);

        // LIST OF 4X1 SPRITES TO PLACE OVER WORK LOG
        List<toolPathSprite> toolPathSpriteList = new List<toolPathSprite>();
        
        // Testpoint
        Texture2D testPointSprite;
       
        public Game1()
        {
            graphics = new GraphicsDeviceManager(this);
            Content.RootDirectory = "Content";
            
        }

        /// <summary>
        /// Allows the game to perform any initialization it needs to before starting to run.
        /// This is where it can query for any required services and load any non-graphic
        /// related content.  Calling base.Initialize will enumerate through any components
        /// and initialize them as well.
        /// </summary>
        protected override void Initialize()
        {
            // TODO: Add your initialization logic here

            base.Initialize();
        }

        /// <summary>
        /// LoadContent will be called once per game and is the place to load
        /// all of your content.
        /// </summary>
        protected override void LoadContent()
        {
            IsMouseVisible = true;
            // SCREEN SIZE RELATED
            graphics.PreferredBackBufferHeight = 720;
            graphics.PreferredBackBufferWidth = 1280;
            //graphics.IsFullScreen = true;
            graphics.ApplyChanges();
            
            // Create a new SpriteBatch, which can be used to draw textures.
            spriteBatch = new SpriteBatch(GraphicsDevice);

            // Load sprites
            toolBG = Content.Load<Texture2D>("Sprites//frontEndBG");
            workLogOne = new workLog((Content.Load<Texture2D>("Sprites//maxWorkLog")), workLogPosition);
            toolOne = new tool((Content.Load<Texture2D>("Sprites//toolOne")), toolPosition);
            testPointSprite = Content.Load<Texture2D>("Sprites//testPoint");

            viewportRect = new Rectangle(0, 0, 1280, 720);
            // TODO: use this.Content to load your game content here
        }

        /// <summary>
        /// UnloadContent will be called once per game and is the place to unload
        /// all content.
        /// </summary>
        protected override void UnloadContent()
        {
            // TODO: Unload any non ContentManager content here
        }

        /// <summary>
        /// Allows the game to run logic such as updating the world,
        /// checking for collisions, gathering input, and playing audio.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Update(GameTime gameTime)
        {
            // Allows the game to exit
            if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
                this.Exit();
            
            KeyboardState keyboardState = Keyboard.GetState();

            // Need to restrict this.
            #region moveTool
            // Move left is key press = left
            if (keyboardState.IsKeyDown(Keys.Left))
            {
                double x = toolOne.moveLeft();
            }
            // Move right is key press = right
            if (keyboardState.IsKeyDown(Keys.Right))
            {
                double x = toolOne.moveRight();
            }
            // Move up is key press = up
            if (keyboardState.IsKeyDown(Keys.Up))
            {
               double y = toolOne.moveUp();
            }
            // Move down is key press = down
           if (keyboardState.IsKeyDown(Keys.Down))
            {
                double y = toolOne.moveDown();
            }
            
            MouseState mouseState = Mouse.GetState();
            if (mouseState.LeftButton == ButtonState.Pressed)
            {
                toolOne.position = new Vector2(mouseState.X, mouseState.Y);
            }


            /*
            // TODO: Add your update logic here
            g = new Galil.Galil();
            g.address = "";
            
            g.command("PA"+ y );
            */

            #endregion

            #region verybadcodeforsideright
            if (keyboardState.IsKeyDown(Keys.M))
            {
                if (miscFunction.IsPointInside(workLogOne, toolOne))
                {
                    float Y1 = workLogOne.position.Y - (workLogOne.sprite.Height / 2) + gVar.workLogPixelHeight;
                    float Y2 = toolOne.position.Y - (toolOne.sprite.Height / 2);
                    int height = (int) (Y1 - Y2);

                    //if (height < 6)
                    //{
                        toolPathSprite tempPathSprite1 = new toolPathSprite(Content.Load<Texture2D>("Sprites//toolPathSideTwo"));
                        toolPathSprite tempPathSprite2 = new toolPathSprite(Content.Load<Texture2D>("Sprites//toolPathSideTwo"));
                        toolPathSprite tempPathSprite3 = new toolPathSprite(Content.Load<Texture2D>("Sprites//toolPathSideTwo"));

                        tempPathSprite1.position = toolOne.position;
                        tempPathSprite2.position = toolOne.position;
                        tempPathSprite3.position = toolOne.position;

                        tempPathSprite1.position.X = toolOne.position.X + toolOne.sprite.Width / 2;
                        tempPathSprite2.position.X = toolOne.position.X + toolOne.sprite.Width / 2;
                        tempPathSprite3.position.X = toolOne.position.X + toolOne.sprite.Width / 2;

                        tempPathSprite1.position.Y = Y2 + 1;
                        tempPathSprite2.position.Y = Y2 + 3;
                        tempPathSprite3.position.Y = Y2 + 5;
                        
                        toolPathSpriteList.Add(tempPathSprite1);
                        toolPathSpriteList.Add(tempPathSprite2);
                        toolPathSpriteList.Add(tempPathSprite3);
                    //}
                
                }
            }
            #endregion

            #region verybadcodeforsideleft
            if (keyboardState.IsKeyDown(Keys.N))
            {
                if (miscFunction.IsPointInside(workLogOne, toolOne))
                {
                    float Y1 = workLogOne.position.Y - (workLogOne.sprite.Height / 2) + gVar.workLogPixelHeight;
                    float Y2 = toolOne.position.Y - (toolOne.sprite.Height / 2);
                    int height = (int)(Y1 - Y2);

                    //if (height < 6)
                    //{
                    toolPathSprite tempPathSprite1 = new toolPathSprite(Content.Load<Texture2D>("Sprites//toolPathSideTwo"));
                    toolPathSprite tempPathSprite2 = new toolPathSprite(Content.Load<Texture2D>("Sprites//toolPathSideTwo"));
                    toolPathSprite tempPathSprite3 = new toolPathSprite(Content.Load<Texture2D>("Sprites//toolPathSideTwo"));

                    tempPathSprite1.position = toolOne.position;
                    tempPathSprite2.position = toolOne.position;
                    tempPathSprite3.position = toolOne.position;

                    tempPathSprite1.position.X = toolOne.position.X - toolOne.sprite.Width / 2;
                    tempPathSprite2.position.X = toolOne.position.X - toolOne.sprite.Width / 2;
                    tempPathSprite3.position.X = toolOne.position.X - toolOne.sprite.Width / 2;

                    tempPathSprite1.position.Y = Y2 + 1;
                    tempPathSprite2.position.Y = Y2 + 3;
                    tempPathSprite3.position.Y = Y2 + 5;

                    toolPathSpriteList.Add(tempPathSprite1);
                    toolPathSpriteList.Add(tempPathSprite2);
                    toolPathSpriteList.Add(tempPathSprite3);
                    //}

                }
            }
            #endregion

            #region paintWorkLog
            if (keyboardState.IsKeyDown(Keys.Space)) 
            {                
                if (miscFunction.IsPointInside(workLogOne, toolOne))
                {
                    toolPathSprite tempPathSprite = new toolPathSprite(Content.Load<Texture2D>("Sprites//toolPathFront"));
                    tempPathSprite.position = toolOne.position;
                    tempPathSprite.position.Y -= toolOne.sprite.Height / 2;
                    toolPathSpriteList.Add(tempPathSprite);
                }
            }
            #endregion

            base.Update(gameTime);
        }

        /// <summary>
        /// This is called when the game should draw itself.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Draw(GameTime gameTime)
        {
            // BOILERPLATE
            GraphicsDevice.Clear(Color.White);
            spriteBatch.Begin(SpriteSortMode.Immediate, BlendState.AlphaBlend);
            
            // DRAW THE BACKGROUND IMAGE
            spriteBatch.Draw(toolBG, viewportRect, Color.White);

            // DRAW THE WORK LOG
            spriteBatch.Draw(workLogOne.sprite, workLogOne.position, null, Color.White, 0.0f, workLogOne.center, 1.0f, SpriteEffects.None, 0);
            
            // TOOL PATH SPRITES
            for (int i = 0; i < toolPathSpriteList.Count; i++)
            {
                spriteBatch.Draw(toolPathSpriteList[i].sprite, toolPathSpriteList[i].position, null, Color.White, 0.0f, toolPathSpriteList[i].center, 1.0f, SpriteEffects.None, 0);
            }

            // DRAW THE TOOL
            spriteBatch.Draw(toolOne.sprite, toolOne.position, null, Color.White, 0.0f, toolOne.center, 1.0f, SpriteEffects.None, 0);


            // Test sprite
            //spriteBatch.Draw(testPointSprite, position, null, Color.White, 0.0f, center, 1.0f, SpriteEffects.None, 0);

            // BOILERPLATE
            spriteBatch.End();
            base.Draw(gameTime);
        }
    }
}

